
%macro print(program=, disname=, idisname=, ds_list=,lnam=, wherefl=);
proc format;
   value $fyn
   'Y' = 'YES'
   'N' = 'NO'
   ;
value treat
  1 = "Treatment A"
  2 = "Treatment B"
  3 = "Treatment C"
  ;
  ;
quit;

%get_data(lst=ae,pre=sdtm,lib=sdtm);

/*
data suae(keep=usubjid aeseq AEEXPECT);
set sdtm.suppae;
aeseq=input(idvarval,best.);
if qnam="AEEXPECT" then AEEXPECT=strip(qnam);
if AEEXPECT ne " ";
run;

proc sort data=suae; by usubjid aeseq; run;
*/

proc sort data=adam.adae out=tosort;
by usubjid aeseq;
where index(saffl,"Y") >0;
run;

data tosort; 
set tosort;
aesdth = ''; 
if &wherefl;
run; 

data info(keep=  subjid ady usubjid pertr socpt astdtm onset resdt ire osd aoac aeseq AECONTRT) ;
   length subjid ady  aecontrt pertr socpt $150. onset resdt ire osd aoac $100. ;
set tosort /*suae(in=cc)*/;
by usubjid aeseq; 

AECONTRT = strip(put(AECONTRT,$fyn.)); 
subjid = substr(usubjid,13); 
ady = put(aestdy, best.); 
if trta ne '' then trta_ = "Product "||strip(trta); 

if trtan in (1 2 3) then part='Part A';
else if trtan in (4 5 6) then part='Part B';

pertr=Strip(part)||"/^n"|| "Day "||strip(ady)||"/^n"||strip(trta_)||"/^n"||strip(aespid);   
socpt=trim(AEBODSYS)||"/^n  "||trim(AEDECOD)||"^n    "||trim(AETERM);
if AESTDTC ne '' and ADURLDC ne '' then onset=strip(substr(aestdtc, 1, 10))||"^n"||scan(aestdtc,2,'T')||"^n("||strip(ADURLDC)||')' ;
if AESTDTC ne '' and ADURLDC = '' then onset=strip(substr(aestdtc, 1, 10))||"^n"||scan(aestdtc,2,'T') ;

if aeendtc ne " " then resdt=strip(substr(aeendtc, 1, 10))||"^n"||scan(aeendtc,2,'T')||"^n("||strip(ADURC)||")";
	else resdt=" ";
ire  = "S: " || strip(asev) || "^nR: "|| strip(AREL)||"^nF: "||strip(AEPATT) ; 
osd  = "O: " || strip(AEOUT)|| "^nS: " || strip(put(aeser,$fyn.)) || "^nD: " || strip(aedisc);
aoac=strip(aeacn)/*||" C: "||strip(put(AECONTRT,$fyn.))*/;
run;
  
Proc sort data = info;
   by SUBJID aeseq;
run;

proc sql;
select count(distinct usubjid)
into :doit
from info;
quit;

%page_cut(in=info,sort=SUBJID aeseq astdtm, columns=, width=, newpage=, nocut=subjid,skip=,pagesize=32,out=REPORT);

title3 " ";
title4 "Table &disname (Continued)";

%footer1(foot1=1, ds_list=&ds_list., Program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..DEATH.AE.rtf";

%open_rtf;
%empty_ds_fix(report);

title5 "&lnam.";
title6 "(Safety Population)";

proc report data=report nowd split='~' missing ;
 column page usubjid SUBJID astdtm pertr socpt  onset resdt ire osd aoac AECONTRT;
 define page      / " " order order=internal noprint;
 define USUBJID   / " " order order=internal noprint;
 define SUBJID    / "Subject~Id" order order=internal style(column)=[cellwidth=0.6in just=l] style(header)=[just=l];
 define astdtm    / " " order order=internal noprint;
 define pertr     / "Study~Part/Day/~Study~Product/~AE #" order order=internal style(column)=[cellwidth=0.8 in just=l] style(header)=[just=l];

 define socpt    / "SOC/~MedDRA Preferred~Term~Description of~AE" order style(column)=[cellwidth=1.4in just=l] style(header)=[just=l];
 define onset    / "Onset Date/Time~(Time since~Last Dose)"  style(column)=[cellwidth=1.3in just=l] style(header)=[just=l];
 define resdt    / "Resolution~Date/Time~(Duration)"  style(column)=[cellwidth=1.2in just=l] style(header)=[just=l];
 define ire      / "S: Severity~R: Relationship to~Study Product~F: Frequency"  style(column)=[cellwidth=1.1in just=l] style(header)=[just=l];
 define osd      / "O: Outcome~S: Serious AE~D: AE Lead to~Discontinuation"  style(column)=[cellwidth=1.4in just=l] style(header)=[just=l];
 define aoac     / "Action Taken~With Product~Use / Was~this AE a~result of~Study~Medication~error?"  style(column)=[cellwidth=1.0in just=l] style(header)=[just=l];
define AECONTRT     / "Was a~concomitant~treatment~given because~of the~occurrence of the event? /~CM #"  style(column)=[cellwidth=1.0in just=l] style(header)=[just=l];

 break after page   / page;

 compute before USUBJID;
  line "   ";
 endcomp;


%if &doit EQ 0 %then %do;
compute after page/style=[cellwidth=9.7in just=left];
   line "No subject was found for this listing.";
endcomp;
     %end;
run;

%close_rtf;
%arrange_rtf;

%mend print;



%print(program=T14_03_02_01_DEATH_AE, disname=14.3.2.1, idisname=14.3.2.1, ds_list=ADAE, 
lnam=%str(Listing of Deaths, Other Serious and Significant Adverse Events),
wherefl=%str(AESER="Y" or AESDTH ^=''));

%clrw;


